/**
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the latest version of the GNU Lesser General
 * Public License as published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program (LICENSE.txt); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.lightframework.jamwiki.servlets;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jamwiki.WikiBase;
import org.jamwiki.WikiMessage;
import org.jamwiki.servlets.JAMWikiServlet;
import org.jamwiki.servlets.WikiPageInfo;
import org.jamwiki.utils.WikiUtil;
import org.lightframework.jamwiki.model.VirtualWiki;
import org.springframework.web.servlet.ModelAndView;

/**
 * dashboard is the home page of jamwiki to list all the virtual wikis.
 * 
 * @author fenghm(live.fenghm@gmail.com)
 */
public class DashboardServlet extends JAMWikiServlet {
	
	public static final String IS_DASHBOARD_PAGE_KEY = "isDishboardPage";

	@Override
    protected ModelAndView handleJAMWikiRequest(HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo)
            throws Exception {

		next.setViewName("dashboard");
		next.addObject(IS_DASHBOARD_PAGE_KEY,true);
		//next.addObject("changes",getRecentChanges(request, response, next, pageInfo));
		
		pageInfo.setSpecial(true);
		pageInfo.setPageTitle(new WikiMessage("uesrmenu.dashboard"));
		
		WikiContext.setIfNotSet(request, next, pageInfo);
		
		List<VirtualWiki> wikis = new ArrayList<VirtualWiki>();;
		if(!WikiUtil.isFirstUse()){
			List<org.jamwiki.model.VirtualWiki> jamwikis = WikiBase.getDataHandler().getVirtualWikiList();
			for(org.jamwiki.model.VirtualWiki jamwiki : jamwikis){
				VirtualWiki wiki = new VirtualWiki(jamwiki);
				if(!wiki.isSpecial()){
					wikis.add(wiki);
				}
			}
		}

		next.addObject("wikis", wikis);

		return next;
    }
	
	/*
	protected List<RecentChange> getRecentChanges(HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo) throws DataAccessException{
		String virtualWiki = pageInfo.getVirtualWikiName();
		Pagination pagination = new Pagination(15, 0);
		return WikiBase.getDataHandler().getRecentChanges(virtualWiki, pagination, true);
	}
	*/
}
